const utils = require("../../utils/util")

// component/quickGroup/quickGroup.js
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        item: {
            type: Object,
            value: {}
        },
        isUnload: {
            type: Boolean,
            value: false
        }
    },

    /**
     * 组件的初始数据
     */
    data: {
        interval: null,
    },

    lifetimes: {
        created: function () {

        },
        attached: function () {
            // 在组件实例进入页面节点树时执行
            // if (this.data.item && this.data.item.EndTime > 0) {
                // this.countdown()
            // } else {
            //     console.log('已过期，不显示该项')
            //     clearInterval(this.data.interval)
            // }

            this.timer = setInterval(this.countdown.bind(this), 1000)

            if (this.data.isUnload) {
                clearInterval(this.timer)
            }
        },
        ready: function () {

        },
        detached: function () {
            // 在组件实例被从页面节点树移除时执行
            this.timer && clearInterval(this.timer)
        },
    },

    /**
     * 组件的方法列表
     */
    methods: {
        // 倒计时
        countdown() {
            // console.log(this.data.item,'this.data.item')
            const countdownTime = utils.countdownTime(this.data.item.EndTime)
            // console.log(countdownTime,'countdowntime')
            this.setData({ countdownTime })
            if (countdownTime.days <= 0 && countdownTime.hours <= 0 && countdownTime.minutes <= 0 && countdownTime.seconds <= 0) {
                this.timer && clearInterval(this.timer)
            }
        },

        toGroupDetail() {
            let data = this.data.item;
            this.triggerEvent('toGroupDetail', data)
            clearInterval(this.data.interval)
        },

        clearTimer() {
            clearInterval(this.timer)
        }

    },





})
